package com.lekang.aspect;

import com.lekang.common.core.domain.model.LoginUser;
import com.lekang.common.utils.SecurityUtils;
import com.lekang.domain.AccraditationRecord;
import com.lekang.domain.Elderly;
import com.lekang.domain.Message;
import com.lekang.service.IAccraditationRecordService;
import com.lekang.service.IElderlyService;
import com.lekang.service.IMessageService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 审批记录新增方法后置增强切面
 */
@Aspect
@Component
public class MessageAspect {

    private static final Logger log = LoggerFactory.getLogger(MessageAspect.class);

    @Autowired
    private IMessageService messageService;

    @Autowired
    private IElderlyService elderlyService;

    @Autowired
    private IAccraditationRecordService accraditationRecordService;
    /**
     * 定义切入点：匹配AccraditationRecordController的add方法
     */
    @Pointcut("execution(* com.lekang.controller.AccraditationRecordController.add(..))")
    public void accraditationAddPointcut() {}


    @Pointcut("execution(* com.lekang.controller.AccraditationRecordController.edit(..))")
    public void accraditationEditPointcut() {}

    /**
     * 后置增强：在审批记录新增方法成功执行后获取参数信息
     * @param joinPoint 连接点对象，用于获取方法参数
     * @param result 方法返回值
     */
//    @AfterReturning(pointcut = "accraditationAddPointcut()", returning = "result")
//    public void afterAdd(JoinPoint joinPoint, Object result) {
//        // 获取方法参数
//        Object[] args = joinPoint.getArgs();
//        if (args != null && args.length > 0 && args[0] instanceof AccraditationRecord) {
//            AccraditationRecord accraditationRecord = (AccraditationRecord) args[0];
//
//            // 打印审批记录新增的参数信息
//            log.info("审批记录新增成功，参数信息如下：");
//            log.info("主键ID：{}", accraditationRecord.getId());
//            log.info("审批意见：{}", accraditationRecord.getOpinion());
//            log.info("审批类型(1:退住,2:请假,3:入住)：{}", accraditationRecord.getType());
//            log.info("审批人：{}", accraditationRecord.getApproverName());
//            log.info("审批人角色：{}", accraditationRecord.getApproverNameRole());
//            log.info("业务ID：{}", accraditationRecord.getBussniessId());
//            log.info("审核状态(1:通过,2:拒绝,3:驳回,4:撤回,5:撤销)：{}", accraditationRecord.getAuditStatus());
//            log.info("审核步骤：{}", accraditationRecord.getStepNo());
//            log.info("处理类型{}",accraditationRecord.getHandleType());
//
//            System.out.println("审批记录ID：" + accraditationRecord.getId() + " 新增成功");
//            Elderly elderly = elderlyService.selectElderlyById(accraditationRecord.getCheckContentId());
//            if (elderly == null) {
//                return;
//            }
//            Message message = new Message();
//            message.setMessageType("0");
//            message.setCreateTime(new Date());
//            if(accraditationRecord.getType().equals("入住")){
//                if(accraditationRecord.getHandleType().equals("申请中")){
//                    message.setTitle("【待办事项】 您有一个单据待处理");
//                    message.setContent(accraditationRecord.getBussniessId()+" "+elderly.getName()+"的入住申请");
//                }else {
//                    message.setContent(null);
//                }
//            }else if (accraditationRecord.getType().equals("退住")){
//                if (accraditationRecord.getHandleType().equals("申请中")){
//                    message.setTitle("【待办事项】 您有一个单据待处理");
//                    message.setContent(accraditationRecord.getBussniessId()+" "+elderly.getName()+"的退住申请");
//                }else {
//                    message.setContent(null);
//                }
//            }else if (accraditationRecord.getType().equals("请假")){
//                if (accraditationRecord.getHandleType().equals("申请中")){
//                    message.setTitle("【待办事项】 您有一个单据待处理");
//                    message.setContent(accraditationRecord.getBussniessId()+" "+elderly.getName()+"的请假申请");
//                }else {
//                    message.setContent(null);
//                }
//            }
//
//            if (message.getContent()!=null && !message.getContent().equals("")) {
//                int i = messageService.insertMessage(message);
//                System.out.println(111111);
//            }
//
//        }
//    }
//
//    //修改表时
//
//    @AfterReturning(pointcut = "accraditationEditPointcut()", returning = "result")
//    public void afterEdit(JoinPoint joinPoint, Object result) {
//        // 获取方法参数
//        Object[] args = joinPoint.getArgs();
//        if (args != null && args.length > 0 && args[0] instanceof AccraditationRecord) {
//            AccraditationRecord accraditationRecord = (AccraditationRecord) args[0];
//
//            // 打印审批记录修改的参数信息
//            log.info("审批记录修改成功，参数信息如下：");
//            log.info("主键ID：{}", accraditationRecord.getId());
//            log.info("审批意见：{}", accraditationRecord.getOpinion());
//            log.info("审批类型(1:退住,2:请假,3:入住)：{}", accraditationRecord.getType());
//            log.info("审批人：{}", accraditationRecord.getApproverName());
//            log.info("审批人角色：{}", accraditationRecord.getApproverNameRole());
//            log.info("业务ID：{}", accraditationRecord.getBussniessId());
//            log.info("审核状态(1:通过,2:拒绝,3:驳回,4:撤回,5:撤销)：{}", accraditationRecord.getAuditStatus());
//            log.info("审核步骤：{}", accraditationRecord.getStepNo());
//            log.info("处理类型{}",accraditationRecord.getHandleType());
//
//            System.out.println("审批记录ID：" + accraditationRecord.getId() + " 修改成功");
//            AccraditationRecord accraditationRecordOld = accraditationRecordService.selectAccraditationRecordById(accraditationRecord.getId());
//
//            if(accraditationRecord.getHandleType()!=null && !accraditationRecord.getHandleType().equals("")){
//                accraditationRecordOld.setHandleType(accraditationRecord.getHandleType());
//            }
//            Elderly elderly = elderlyService.selectElderlyById(accraditationRecordOld.getCheckContentId());
//            if (elderly == null) {
//                return;
//            }
//            Message message = new Message();
//            message.setMessageType("0");
//            message.setCreateTime(new Date());
//            System.out.println(accraditationRecordOld.toString()+"测似乎");
//            if(accraditationRecordOld.getType().equals("入住")){
//                if(accraditationRecordOld.getHandleType().equals("已拒绝")){
//                    message.setTitle("【已批事项】 您有一个申请已拒绝");
//                    message.setContent(accraditationRecordOld.getBussniessId()+" "+elderly.getName()+"的入住申请");
//                }else if(accraditationRecordOld.getHandleType().equals("已审批")){
//                    message.setTitle("【已批事项】 您有发起的单据已审批");
//                    message.setContent(accraditationRecordOld.getBussniessId()+" "+elderly.getName()+"的入住申请");
//                }else if(accraditationRecordOld.getHandleType().equals("申请中")){
//                    message.setTitle("【代办事项】 您有发起的单据申请中");
//                    message.setContent(accraditationRecordOld.getBussniessId()+" "+elderly.getName()+"的入住申请");
//                }else {
//                    message.setContent(null);
//                }
//            }else if (accraditationRecordOld.getType().equals("退住")){
//                if (accraditationRecordOld.getHandleType().equals("已拒绝")){
//                    message.setTitle("【已批事项】 您有一个申请已拒绝");
//                    message.setContent(accraditationRecordOld.getBussniessId()+" "+elderly.getName()+"的退住申请");
//                }else if(accraditationRecordOld.getHandleType().equals("已审批")){
//                    message.setTitle("【已批事项】 您发起的单据已审批");
//                    message.setContent(accraditationRecordOld.getBussniessId()+" "+elderly.getName()+"的退住申请");
//                }else {
//                    message.setContent(null);
//                }
//            }else if (accraditationRecordOld.getType().equals("请假")){
//                if (accraditationRecordOld.getHandleType().equals("已拒绝")){
//                    message.setTitle("【已批事项】 您有一个单据已拒绝");
//                    message.setContent(accraditationRecordOld.getBussniessId()+" "+elderly.getName()+"的请假申请");
//                }else if(accraditationRecordOld.getHandleType().equals("已审批")){
//                    message.setTitle("【已批事项】 您发起的单据已审批");
//                    message.setContent(accraditationRecordOld.getBussniessId()+" "+elderly.getName()+"的请假申请");
//                }else {
//                    message.setContent(null);
//                }
//            }
//
//            if (message.getContent()!=null && !message.getContent().equals("") && accraditationRecord.getAuditStatus().equals("申请中")) {
//                int i = messageService.insertMessage(message);
//                System.out.println(111111);
//            }
//
//        }
//    }

    @AfterReturning(pointcut = "accraditationAddPointcut()", returning = "result")
    public void afterAdd(JoinPoint joinPoint, Object result) {
        // 获取方法参数
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0 && args[0] instanceof AccraditationRecord) {
            AccraditationRecord accraditationRecord = (AccraditationRecord) args[0];

            System.out.println("审批记录ID：" + accraditationRecord.getId() + " 新增成功");

            Message message = new Message();
            message.setMessageType("0");
            message.setCreateTime(new Date());
            if(accraditationRecord.getType().equals("入住")){
                message.setTitle("【待办事项】 您有一个单据待处理");
                message.setContent("入住申请");
            }else if (accraditationRecord.getType().equals("退住")){
                message.setTitle("【待办事项】 您有一个单据待处理");
                message.setContent("退住申请");
            }else if (accraditationRecord.getType().equals("请假")){
                message.setTitle("【待办事项】 您有一个单据待处理");
                message.setContent("请假申请");
            }

                int i = messageService.insertMessage(message);
                System.out.println(111111);

        }
    }

    //修改表时

//    @AfterReturning(pointcut = "accraditationEditPointcut()", returning = "result")
//    public void afterEdit(JoinPoint joinPoint, Object result) {
//        // 获取方法参数
//        Object[] args = joinPoint.getArgs();
//        if (args != null && args.length > 0 && args[0] instanceof AccraditationRecord) {
//            AccraditationRecord accraditationRecord = (AccraditationRecord) args[0];
//
//            System.out.println("审批记录ID：" + accraditationRecord.getId() + " 新增成功");
//
//            Message message = new Message();
//            message.setMessageType("0");
//            message.setCreateTime(new Date());
//            if(accraditationRecord.getType().equals("入住")){
//                message.setTitle("【待办事项】 您有一个单据待处理");
//                message.setContent("入住申请");
//            }else if (accraditationRecord.getType().equals("退住")){
//                message.setTitle("【待办事项】 您有一个单据待处理");
//                message.setContent("退住申请");
//            }else if (accraditationRecord.getType().equals("请假")){
//                message.setTitle("【待办事项】 您有一个单据待处理");
//                message.setContent("请假申请");
//            }
//
//            int i = messageService.insertMessage(message);
//            System.out.println(111111);
//
//        }
//    }
}
